﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;
using CommunitySystem.Views;

namespace CommunitySystem.PageView
{
    public partial class BianZhiEmpDetailControl : AsyncCtrl
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        EmpbasicService.BianZhiModel RoleModel = new EmpbasicService.BianZhiModel();
        private int OrgID = 0;

        private ObservableCollection<EmpbasicService.Emp_BasicInfoModel> EmpModels =
            new ObservableCollection<EmpbasicService.Emp_BasicInfoModel>();
        public ObservableCollection<EmpbasicService.BianZhiModel> BianZhiModels =
            new ObservableCollection<EmpbasicService.BianZhiModel>();
        public BianZhiEmpDetailControl(int orgid,string type,string keshiname,string zaizhijianchi)
        {
            InitializeComponent();
            PeiZhi.Visibility = System.Windows.Visibility.Collapsed;
            Client.GetEmp_BasicInfoModelByKeShiCompleted += 
                new EventHandler<EmpbasicService.GetEmp_BasicInfoModelByKeShiCompletedEventArgs>(Client_GetEmp_BasicInfoModelByKeShiCompleted);
            Client.GetEmp_BasicInfoModelByKeShiAsync(orgid, type, keshiname, zaizhijianchi,"");
            PeiZhi.Visibility = GuiHuan.Visibility = System.Windows.Visibility.Collapsed;
            Client.TuiHuanZhiShuCompleted += new EventHandler<TuiHuanZhiShuCompletedEventArgs>(Client_TuiHuanZhiShuCompleted);
            PeiZhi.Visibility = System.Windows.Visibility.Collapsed;
        }

        private ObservableCollection<EmpbasicService.Emp_BasicInfoModel> Models =
            new ObservableCollection<EmpbasicService.Emp_BasicInfoModel>();
        private string ZhiWeiType;
        private int KeShiID;
        private int RenShu;
        private string Type;
        public BianZhiEmpDetailControl(ObservableCollection<EmpbasicService.Emp_BasicInfoModel> models,string type,int renshu,int orgid,string zhiweitype,int keshiid)
        {
            InitializeComponent();
            Models = models;
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = models;
            EmpModels = models;
            ZhiWeiType = zhiweitype;
            KeShiID = keshiid;
            RenShu = renshu;
            Type = type;
            OrgID = orgid;
            PeiZhi.Visibility = System.Windows.Visibility.Collapsed;
            GuiHuan.Visibility = System.Windows.Visibility.Collapsed;
            Client.GetEmpIds_guoduCompleted += new EventHandler<GetEmpIds_guoduCompletedEventArgs>(Client_GetEmpIds_guoduCompleted);
            Client.GetEmpIdsCompleted += new EventHandler<GetEmpIdsCompletedEventArgs>(Client_GetEmpIdsCompleted);
            if (type == "过渡性")
            {
                GuiHuan.Visibility = System.Windows.Visibility.Visible;
                if (renshu > models.Count)
                {
                    PeiZhi.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    PeiZhi.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            Client.TuiHuanZhiShuCompleted += new EventHandler<TuiHuanZhiShuCompletedEventArgs>(Client_TuiHuanZhiShuCompleted);
        }

        

        void Client_GetEmp_BasicInfoModelByKeShiCompleted(object sender, EmpbasicService.GetEmp_BasicInfoModelByKeShiCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        private void Data_Click(object sender, RoutedEventArgs e)
        {
            var seljw = DGRole.SelectedItem as EmpbasicService.Emp_BasicInfoModel;
            if (seljw != null)
            {
                JSOtherEmpInfo win = new JSOtherEmpInfo(seljw.ID);
                win.Show();
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个人员进行查看！");
            }
        }

        private void Data1_Click(object sender, RoutedEventArgs e)
        {
            BianZhi_AnPaiRenYuanWindow win = new BianZhi_AnPaiRenYuanWindow(OrgID, ZhiWeiType, KeShiID, RenShu, Type,Models);
            win.Show();
        }

        private void Data3_Click(object sender, RoutedEventArgs e)
        {
            var sel = DGRole.SelectedItem as EmpbasicService.Emp_BasicInfoModel;
            if(sel != null)
            {
                if(MessageBox.Show("确定归还过渡性职数吗？","提示",MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Client.TuiHuanZhiShuAsync(0, sel.ID);
                }
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个人员进行职数归还！");
            }
        }

        void Client_TuiHuanZhiShuCompleted(object sender, TuiHuanZhiShuCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("退还成功！");
            if(Type == "过渡性")
            {
                Client.GetEmpIds_guoduAsync(KeShiID, Type, 1);
            }
            if(Type == "借用")
            {
                Client.GetEmpIdsAsync(KeShiID, Type, 0);
            }
        }

        void Client_GetEmpIdsCompleted(object sender, GetEmpIdsCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        void Client_GetEmpIds_guoduCompleted(object sender, GetEmpIds_guoduCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }
    }
}
